package org.activequant.dao.hibernate.types;



/**
 * Hibernate helper to convert Enum to/from plain VARCHAR type
 * instead of VARCHAR FOR BIT DATA(255).<br>
 * An abstract class. EnumType&lt;E extends Enum&lt;E&gt;&gt; extends SimpleType.<br/>
 * Holds the following associated variable:
 * <ul>
 * <li>clazz(Class&lt;E&gt;)</li>
 * </ul>
 * Holds the following inherited variable:
 * <ul>
 * <li>sqlTypes(int[])</li>
 * </ul>
 * <b>History:</b><br>
 *  - [01.11.2007] Created (Erik Nijkamp)<br>
 *
 *  @author Erik Nijkamp
 */
public abstract class EnumType<E extends Enum<E>> extends SimpleType {
	/**
	 * private Class&lt;E&gt; clazz = null;
	 */
    private Class<E> clazz = null;
    /**
     * Constructs an EnumType&lt;E extends Enum&lt;E&gt;&gt;(extends SimpleType) using the given c(Class&lt;E&gt;) to set its associated clazz(Class&lt;E&gt;)
     * @param c
     */
    protected EnumType(Class<E> c) { 
        this.clazz = c; 
    }
    /**
     * returns the associated clazz(Class&lt;E&gt;)
     */
	public Class<E> returnedClass() {
		return clazz;
	}

    /**
     * returns the name(String) of the given obj(Object) (must be an Enum&lt;E&gt;)
     */
	@Override
    @SuppressWarnings("unchecked")
	protected String format(Object obj) {
		return ((Enum<E>) obj).name();
	}
	/**
	 * Parses and returns an Enum Object (of the associated clazz(Class&lt;E&gt;)) from the given val(String)
	 */
	@Override
	protected Object parse(String val) {
		return Enum.valueOf(clazz, val);
	}
} 
